package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func getIntersectionNode(headA, headB *ListNode) *ListNode {
	m := make(map[*ListNode]struct{})
	p := headA
	for p != nil {
		m[p] = struct{}{}
		p = p.Next
	}
	p = headB
	for p != nil {
		if _, ok := m[p]; ok {
			return p
		}
		p = p.Next
	}
	return nil
}